package com.matt.repository.impl;

import com.matt.foundation.DO.ClassesTopicDO;
import com.matt.foundation.DO.CoursePlanNurseryClassesDo;
import com.matt.repository.base.BaseNativeSqlRepository;
import com.matt.repository.base.ClassesCourseTopicRepository;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2018/3/19
 * *******************************
 */
@Component
public class ClassesCourseTopicRepositoryImpl extends BaseNativeSqlRepository implements ClassesCourseTopicRepository{
    @Override
    public List<ClassesTopicDO> findByTopic(String topicId) {
        String findSql = String.format("select c.id as classesId, c.areaId as areaId, c.name as classesName "
                + " from rel_classes_course_topic cct left join classes c on cct.classesId = c.id where cct.topicId = '%s' and c.deleted='false' ", topicId);
        List<ClassesTopicDO> objectArrayList = sqlObjectList(findSql, ClassesTopicDO.class);
        return objectArrayList;
    }

    @Override
    public List<ClassesTopicDO> findByTopic(String topicId, String areaId) {
        String findSql = String.format("select c.id as classesId, c.areaId as areaId, c.name as classesName "
                + " from rel_classes_course_topic cct left join classes c on cct.classesId = c.id where cct.topicId = '%s' and c.areaId ='%s'", topicId, areaId);
        List<ClassesTopicDO> objectArrayList = sqlObjectList(findSql, ClassesTopicDO.class);
        return objectArrayList;
    }

    @Transactional
    @Override
    public Integer deleteClassTopicAfter(String classId, String today) {
        String deleteSql = String.format("delete from rel_classes_course_topic where id in " +
                " (select id from rel_classes_course_topic rel left join course_topic t on rel.topicId = t.id " +
                " where ct.endDate >= '%s') ", today);

        Integer result = sqlResult(deleteSql);
        return result;
    }
}

